package com.lanou.dllo.findfood.adpter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.entity.HomeLastResult;
import com.lanou.dllo.findfood.util.MyItemClickListener;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.List;

/**
 * Created by dllo on 15/10/21.
 */
public class HomeLastReclyerAdpter extends RecyclerView.Adapter
        <HomeLastReclyerAdpter.LastReclyerAdpter> {
    private List<HomeLastResult> lastResults;
    private RequestQueue queue;
    private LruCache<String, Bitmap> lruCache;
    private ImageLoader.ImageCache imageCache;
    private ImageLoader imageLoader;
    private MyItemClickListener myItemClickListener;


    public void addData(List<HomeLastResult> results) {
        lastResults = results;
        notifyDataSetChanged();

    }

    //对图片的监听
    public void setImageListener(MyItemClickListener listener) {

        myItemClickListener = listener;
    }

    //图片二级缓存
    public HomeLastReclyerAdpter(Context context) {
        queue = Volley.newRequestQueue(context);
        lruCache = new LruCache<String, Bitmap>(50);
        imageCache = new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url) {
                return lruCache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {

                lruCache.put(url, bitmap);
            }
        };
        imageLoader = new ImageLoader(queue, imageCache);


    }

    private void getBitmap(String url, final NetworkImageView networkImageView) {
        ImageLoader imageLoader = VolleySington.getInstance().getImageLoader();
        networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
        networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
        networkImageView.setImageUrl(url, imageLoader);


    }


    //加载行布局
    @Override
    public LastReclyerAdpter onCreateViewHolder(ViewGroup parent, int position) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.rececy_home_item, null);
        return new LastReclyerAdpter(v);
    }


    @Override
    public void onBindViewHolder(LastReclyerAdpter holder, int position) {
        if (lastResults != null && lastResults.size() > 0) {
            HomeLastResult result = lastResults.get(position);
            holder.place_name.setText(result.getPlace_name());
           holder.time.setText(result.getPublish_time());

//            String tim = result.getPublish_time();
//            Log.d("HomeLastReclyerAdpter", "tim:====>"+tim);
//            Date date = TimeUtil.getTimeFromString(tim,"yy-MM-dd hh:mm:ss");
//            holder.time.setText(TimeUtil.getDescriptionTimeFromTimestamp(date.getTime()));
//
//           // String tim = "2014-5-9";
//            long l = Float.valueOf(tim).longValue();
//              holder.time.setText(getDescriptionTimeFromTimestamp(l) + "");


            String comment = result.getComment();
            if (comment != null && comment.length() > 0) {
                holder.comment.setText(result.getComment());
            } else if (comment == null) {
                String str = "味道不错哟!!推荐大家去尝一尝O(∩_∩)O~";
                holder.comment.setText(str);
            }
            holder.user_name.setText(result.getUser_name());
            holder.food_name.setText(result.getFood_name());
            if (result.getPicture_url() != null && result.getPicture_url().length() > 0) {
                String url = result.getPicture_url();
                getBitmap(url, holder.picture_url);

            }
            if (result.getUser_image() != null && result.getUser_image().length() > 0) {
                String userUrl = result.getUser_image();
                getBitmap(userUrl, holder.user_image);
            }



         //   holder.time.setText( getDescriptionTimeFromTimestamp(result.getPublish_time()));
           // time
            holder.position = position;
        }


    }



    @Override
    public int getItemCount() {
        return lastResults != null && lastResults.size() > 0 ? lastResults.size() : 0;
    }


    class LastReclyerAdpter extends RecyclerView.ViewHolder implements View.OnClickListener {
        private NetworkImageView picture_url;

        private NetworkImageView user_image;
        private TextView food_name, comment, user_name, place_name,time;
        private int position;

        //初始化行布局中的组件
        public LastReclyerAdpter(View itemView) {
            super(itemView);
            picture_url = (NetworkImageView) itemView.findViewById(R.id.picture_url);
            food_name = (TextView) itemView.findViewById(R.id.food_name);
            comment = (TextView) itemView.findViewById(R.id.comment);
            user_name = (TextView) itemView.findViewById(R.id.user_name);
            place_name = (TextView) itemView.findViewById(R.id.place_name);
            user_image = (NetworkImageView) itemView.findViewById(R.id.user_image);
            picture_url.setOnClickListener(this);
            time = (TextView) itemView.findViewById(R.id.mouth);

        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.picture_url:
                    if (lastResults != null && lastResults.size() > 0) {
                        HomeLastResult lastResult = lastResults.get(position);

                        if (myItemClickListener != null) {
                            myItemClickListener.onItemClick(lastResult);//实现该监听方法
                        }


                    }

                    break;
                default:
                    break;
            }

        }
    }
}
